<template>
  <div class="feedback-container">
    <h2>意见反馈</h2>
    <el-form
      ref="formRef"
      :model="feedbackForm"
      :rules="rules"
      label-width="100px"
      v-loading="loading"
    >
      <el-form-item label="反馈类型" prop="type">
        <el-select v-model="feedbackForm.type" placeholder="请选择反馈类型">
          <el-option label="功能建议" value="feature" />
          <el-option label="问题反馈" value="bug" />
          <el-option label="其他" value="other" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="反馈内容" prop="content">
        <el-input
          v-model="feedbackForm.content"
          type="textarea"
          rows="4"
          placeholder="请详细描述您的反馈内容"
        />
      </el-form-item>
      
      <el-form-item label="联系方式" prop="contact">
        <el-input 
          v-model="feedbackForm.contact" 
          placeholder="请留下您的联系方式，如邮箱（选填）" 
        />
        <div class="form-tip">
          您的反馈将发送至系统管理员邮箱
        </div>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitFeedback" :disabled="loading">提交反馈</el-button>
        <el-button @click="resetForm" :disabled="loading">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 成功提示 -->
    <el-dialog v-model="showSuccess" title="提交成功" width="30%" center>
      <div class="success-content">
        <el-icon class="success-icon"><circle-check-filled /></el-icon>
        <p>您的反馈已成功提交！感谢您的宝贵意见。</p>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="showSuccess = false">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { CircleCheckFilled } from '@element-plus/icons-vue'
import { sendFeedback } from '../api'

const formRef = ref<FormInstance>()
const loading = ref(false)
const showSuccess = ref(false)

const feedbackForm = ref({
  type: '',
  content: '',
  contact: ''
})

const rules: FormRules = {
  type: [{ required: true, message: '请选择反馈类型', trigger: 'change' }],
  content: [
    { required: true, message: '请输入反馈内容', trigger: 'blur' },
    { min: 5, message: '反馈内容不能少于5个字符', trigger: 'blur' }
  ],
  contact: [
    { 
      pattern: /^$|^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/, 
      message: '请输入正确的邮箱格式', 
      trigger: 'blur' 
    }
  ]
}

const submitFeedback = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 发送反馈到后端
        const response = await sendFeedback({
          type: feedbackForm.value.type,
          content: feedbackForm.value.content,
          contact: feedbackForm.value.contact
        })
        
        if (response.success) {
          showSuccess.value = true
          resetForm()
        } else {
          ElMessage.error(response.message || '提交失败，请稍后重试')
        }
      } catch (error) {
        console.error('提交反馈失败:', error)
        ElMessage.error('提交失败，请稍后重试')
      } finally {
        loading.value = false
      }
    }
  })
}

const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
}
</script>

<style scoped>
.feedback-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
}

.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 5px;
}

.success-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.success-icon {
  font-size: 48px;
  color: #67C23A;
  margin-bottom: 15px;
}

.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>